﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;

namespace MobilesApplication
{
    public partial class HoaDon : Form
    {
        public SqlConnection myConnection;
        public SqlCommand myCommand;
        public HoaDon()
        {
            InitializeComponent();
        }
        public void KetNoi()
        {
            string strconnect = @"server=.\SQLEXPRESS; database=MobilesApplication; integrated security=true";
            myConnection = new SqlConnection(strconnect);
            myCommand = myConnection.CreateCommand();
        }
        private void HoaDon_Load(object sender, EventArgs e)
        {
            btnList_Click(sender, e);
           
        }
        private void AddCot()
        {
            DataGridViewTextBoxColumn cot0, cot1, cot2, cot3, cot4;
            cot0 = new DataGridViewTextBoxColumn();
            cot0.HeaderText = "STT";
            cot0.Width = 40;

            cot1 = new DataGridViewTextBoxColumn();
            cot1.HeaderText = "Tên Sản Phẩm";
            cot1.Width = 120;

            cot2 = new DataGridViewTextBoxColumn();
            cot2.HeaderText = "Đơn giá";
            cot2.Width = 100;

            cot3 = new DataGridViewTextBoxColumn();
            cot3.HeaderText = "Số lượng";
            cot3.Width = 40;

            cot4 = new DataGridViewTextBoxColumn();
            cot4.HeaderText = "Thành tiền";
            cot4.Width = 120;

            gvSanPhamMua.Columns.AddRange(new DataGridViewColumn[] { cot0, cot1, cot2, cot3, cot4 });
            gvSanPhamMua.RowHeadersVisible = false;

        }
        private void btnThemNhanh_Click(object sender, EventArgs e)
        {
            
        }

       
        private void btnThemSP_Click(object sender, EventArgs e)
        {
            gvSanPhamMua.Rows.Add("",gvSanPham.CurrentRow.Cells[1].Value, gvSanPham.CurrentRow.Cells[2].Value);
            for (int i = 0; i < gvSanPhamMua.Rows.Count; i++)
            {
                gvSanPhamMua.Rows[i].Cells[0].Value = i + 1;
            }
        }

        private void btnLoaiSP_Click(object sender, EventArgs e)
        {
            if (this.gvSanPhamMua.SelectedRows.Count > 0 && this.gvSanPhamMua.SelectedRows[0].Index != this.gvSanPhamMua.Rows.Count - 1)
            {
                this.gvSanPhamMua.Rows.RemoveAt(this.gvSanPhamMua.SelectedRows[0].Index);
                for (int i = gvSanPhamMua.Rows.Count-1; i>=0 ; i--)
                {
                    gvSanPhamMua.Rows[i].Cells[0].Value = i+1;
                }
            }
        }

        private void btnLoaiHetSP_Click(object sender, EventArgs e)
        {
            gvSanPhamMua.Rows.Clear();
        }
       
        private void gvSanPhamMua_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (gvSanPhamMua.CurrentRow.Cells[2].Value != null && gvSanPhamMua.CurrentRow.Cells[3].Value != null && gvSanPhamMua.CurrentRow.Cells[3].Value.ToString() != "")
                gvSanPhamMua.CurrentRow.Cells[4].Value = (double)(int.Parse(gvSanPhamMua.CurrentRow.Cells[3].Value.ToString()) * double.Parse(gvSanPhamMua.CurrentRow.Cells[2].Value.ToString()));
            try
            {
                double tongtien = 0;
                for (int i = 0; i < gvSanPhamMua.Rows.Count - 1; i++)
                    tongtien += double.Parse(gvSanPhamMua.Rows[i].Cells[4].Value.ToString());
                txtTongTien.Text = tongtien.ToString() + " đồng";
            }
            catch
            {
                MessageBox.Show("Lỗi thành tiền!");
            }
        }

        private void gvSanPhamMua_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

       
        DataView dv;
        private void btnList_Click(object sender, EventArgs e)
        {
            KetNoi();
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = "select *FROM SanPham";
            try
            {
                if (this.myConnection.State == ConnectionState.Closed)
                    this.myConnection.Open();
                SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand);
                DataSet ds = new DataSet();
                myDataAdapter.Fill(ds, "SanPham");
                //this.gvSanPham.DataSource = ds.Tables[0];
                dv = new DataView(ds.Tables["SanPham"]);
                this.gvSanPham.DataSource = dv;
                this.myConnection.Close();
                gvSanPham.RowHeadersVisible = false;
            }
            catch
            {
                MessageBox.Show("Lối kết nối");
            }
            AddCot();
        }

        private void txtLocSP_TextChanged(object sender, EventArgs e)
        {
            dv.RowFilter = "[Name] LIKE '%" + txtLocSP.Text + "%'";
        }

        private void btnTaoHD_Click(object sender, EventArgs e)
        {
          
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTongTien_Click(object sender, EventArgs e)
        {
          
        }

       
    }
}
